package com.zx.sdk.league.member;

import android.app.Activity;
import android.content.Context;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.syzj.open.AdManager;
import com.syzj.open.IncentiveAd;
import com.syzj.views.JjSdk;
import com.zx.sdk.ZxSDK;
import com.zx.sdk.listener.BaseZxRewardListener;
import com.zx.sdk.listener.BaseZxSplashListener;
import com.zx.sdk.model.AdInfo;
import com.zx.sdk.model.ReadyAdPosition;
import com.zx.sdk.model.ZxError;

import java.util.HashMap;

public class JiaJi extends BaseLeagueMember<Object, IncentiveAd, Object, Object, Object> {

    @Override
    public String getName() {
        return "jiaji";
    }

    @Override
    protected void onInit(Context context, String appId, boolean initialized) {
        if (!initialized) {
            JjSdk.init(context, appId);
        }
    }

    @Override
    protected void onLoadSplash(AdInfo adInfo, ViewGroup container, String mode, String type, BaseZxSplashListener listener) {

    }

    @Override
    public boolean onShowSplash(AdInfo adInfo, ViewGroup container, @Nullable Object splash, BaseZxSplashListener listener) {
        return false;
    }

    @Override
    public void onLoadReward(Activity context, AdInfo adInfo, String mode, String type, BaseZxRewardListener listener) {
        adInfo.setUid(ZxSDK.getUid());
        JjSdk.requestIncentiveAd(adInfo.getMapPid(), new Gson().toJson(adInfo), new AdManager.IncentiveAdListener() {
            @Override
            public void onError(int i, String s) {
                switch (mode) {
                    case ZxSDK.FETCH_ONLY:
                        listener.onPreLoadADError(JiaJi.this, new ZxError("-1", s), adInfo);
                        break;
                    case ZxSDK.FETCH_AND_SHOW:
                        listener.onNoAD(JiaJi.this, new ZxError("-1", s), adInfo);
                        break;
                }
            }

            @Override
            public void onLoaded(IncentiveAd incentiveAd) {
                listener.onADLoad(JiaJi.this, adInfo);
                switch (mode) {
                    case ZxSDK.FETCH_ONLY:
                        saveReward(adInfo.getMapPid(), incentiveAd);
                        break;
                    case ZxSDK.FETCH_AND_SHOW:
                        incentiveAd.showAd(context);
                        break;
                }
                incentiveAd.setInteractionListener(new IncentiveAd.InteractionListener() {
                    @Override
                    public void onAdShow() {
                        listener.onADExpose(JiaJi.this, adInfo);
                    }

                    @Override
                    public void onAdClick() {
                        listener.onADClick(JiaJi.this, adInfo);
                    }

                    @Override
                    public void onAdClose() {
                    }

                    @Override
                    public void onAdComplete() {
                    }

                    @Override
                    public void onAdError() {
                        listener.onVideoPlayError(JiaJi.this, new ZxError("-1", "onAdError"), adInfo);
                    }

                    @Override
                    public void onAdVerify() {

                    }

                    @Override
                    public void onAdSkipped() {
                        listener.onAdSkip(JiaJi.this, 0, adInfo);
                    }

                    @Override
                    public void onAdCountdownFinish() {
                        listener.onReward(JiaJi.this, new HashMap<>(), adInfo);
                    }
                });
            }
        });
    }

    @Override
    public boolean onShowReward(Activity context, AdInfo adInfo, @Nullable IncentiveAd reward, BaseZxRewardListener listener) {
        if (reward != null) {
            reward.showAd(context);
            return true;
        }
        return false;
    }

    @Override
    public void onLoadInterstitial(Activity context, AdInfo adInfo, String mode, String type, BaseZxRewardListener listener) {

    }

    @Override
    public boolean onShowInterstitial(Activity context, AdInfo adInfo, @Nullable Object interstitial, BaseZxRewardListener listener) {
        return false;
    }

    @Override
    public int getCpmByPid(String type, String pid) {
        return super.getCpmByPid(type, pid);
    }

    @Override
    public boolean isPreResReady(String type, String pid) {
        switch (type) {
            case ZxSDK.SPLASH:
                return splashMap.get(pid) != null;
            case ZxSDK.REWARD:
                IncentiveAd incentiveAd = rewardMap.get(pid);
                return incentiveAd != null && incentiveAd.isAdEnable();
        }
        return false;
    }

    @Override
    public void notifyBiddingWin(String type, String mapPid, @Nullable ReadyAdPosition loser) throws Exception {

    }

    @Override
    public void notifyBiddingLose(String type, String mapPid, ReadyAdPosition winner, ZxSDK.LoseReason reason) throws Exception {

    }

    @Override
    public void destroySplash(Activity activity) {

    }

    @Override
    public void destroyShownSplash(Activity activity, Object splashAd) {

    }

    @Override
    public void destroyReward(Activity activity) {

    }

    @Override
    public void destroyShownReward(Activity activity, IncentiveAd rewardAd) {

    }
}
